package com.app114.app.android.rk;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
import br.com.dina.ui.widget.UITableView;
import br.com.dina.ui.widget.UITableView.ClickListener;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.app114.app.android.rk.conf.Conf;
import com.app114.app.android.rk.utils.DeviceUtil;
import com.app114.app.android.rk.utils.UmengUtil;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.umeng.fb.UMFeedbackService;
import com.umeng.update.UmengDownloadListener;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

public class Setting extends BaseFragmentActivity implements ClickListener, UmengDownloadListener, UmengUpdateListener {
	private UITableView mTableView;
	private IWXAPI mApi;

	@Override
	protected void activitySetup(Bundle savedInstanceState) {
		setContentView(R.layout.activity_settings);
		mTableView = (UITableView) findViewById(R.id.tableView);
		createList();
		mTableView.commit();
	}

	private void createList() {
		mTableView.setClickListener(this);
		mTableView.addBasicItem("设置");
		mTableView.addBasicItem("帐号绑定");
		mTableView.addBasicItem("用户反馈");
		mTableView.addBasicItem("检查更新");
		mTableView.addBasicItem("关于我们");
	}

	@Override
	protected boolean _onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_share, menu);
		return true;
	}

	@Override
	protected boolean _onOptionsItemSelected(MenuItem item) {
//		 DeviceUtil.toast(this, "分享给好友功能开发中") ;
		 mApi = WXAPIFactory.createWXAPI(this, Conf.APP_ID, false);
		 mApi.registerApp(Conf.APP_ID) ;
		
		 WXWebpageObject webpage = new WXWebpageObject();
		 webpage.webpageUrl = Conf.DEFAULT_HOME_PAGE;
		 WXMediaMessage msg = new WXMediaMessage(webpage);
		 msg.title = getResources().getString(R.string.app_name);
		 msg.description = "分享你的乐趣！";
		
		 SendMessageToWX.Req req = new SendMessageToWX.Req();
		 req.transaction = buildTransaction("webpage");
		 req.message = msg;
		 req.scene = SendMessageToWX.Req.WXSceneSession;
		 mApi.sendReq(req);

//		ShareCompat.IntentBuilder.from(this).setType("text/plain").setText(DataFactory.SHARE_INFO + "\r\n" + Conf.URL_APP).startChooser();
		return true;
	}

	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}

	@Override
	protected void activityDestory() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(int index) {
		Intent intent = null;
		switch (index) {
		case 0:
			DeviceUtil.toast(this, "开发中");
			break;
		case 1:
			intent = new Intent(this, AccountActivity.class);
			intent.setAction("帐号绑定");
			startActivity(intent);
			break;
		case 2:
			UMFeedbackService.openUmengFeedbackSDK(this);
			break;
		case 3:
			UmengUtil.update(this);
			break;
		case 4:
			intent = new Intent(mContext, Browser.class);
			intent.setAction(Intent.ACTION_VIEW);
			intent.setData(Uri.parse("file:///android_asset/about.html"));
			startActivity(intent);
			break;

		default:
			break;
		}

	}

	@Override
	public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
		switch (updateStatus) {
		case 0: // has update
			UmengUpdateAgent.showUpdateDialog(mContext, updateInfo);
			break;
		case 1: // has no update
			Toast.makeText(mContext, "没有更新", Toast.LENGTH_SHORT).show();
			break;
		case 2: // none wifi
			Toast.makeText(mContext, "没有wifi连接， 只在wifi下更新", Toast.LENGTH_SHORT).show();
			break;
		case 3: // time out
			Toast.makeText(mContext, "超时", Toast.LENGTH_SHORT).show();
			break;
		}
	}

	@Override
	public void OnDownloadEnd(int result) {
		Toast.makeText(mContext, "download result : " + result, Toast.LENGTH_SHORT).show();
	}

}
